use tauri::{AppHandle, Manager, WebviewWindow};
use tauri_nspanel::{
  cocoa::appkit::NSWindowCollectionBehavior, panel_delegate, ManagerExt, WebviewWindowExt,
};

pub fn init(app_handle: &AppHandle) {
  let window: WebviewWindow = app_handle.get_webview_window("main").unwrap();

  let panel = window.to_panel().unwrap();

  let delegate = panel_delegate!(MyPanelDelegate {
    window_did_become_key,
    window_did_resign_key
  });

  // let handle = app_handle.to_owned();

  delegate.set_listener(Box::new(move |delegate_name: String| {
    match delegate_name.as_str() {
      "window_did_become_key" => {
        // let app_name = handle.package_info().name.to_owned();
        // println!("[info]: {:?} panel becomes key window!", app_name);
      }
      "window_did_resign_key" => {
        // println!("[info]: panel resigned from key window!");
      }
      _ => (),
    }
  }));

  // Set the window to float level
  #[allow(non_upper_case_globals)]
  const NSFloatWindowLevel: i32 = 25;
  panel.set_level(NSFloatWindowLevel);

  #[allow(non_upper_case_globals)]
  const NSWindowStyleMaskNonActivatingPanel: i32 = 1 << 7;
  // Ensures the panel cannot activate the app
  panel.set_style_mask(NSWindowStyleMaskNonActivatingPanel);

  // Allows the panel to:
  // - display on the same space as the full screen window
  // - join all spaces
  panel.set_collection_behaviour(
    NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary
      // | NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces
      | NSWindowCollectionBehavior::NSWindowCollectionBehaviorMoveToActiveSpace,
  );

  panel.set_delegate(delegate);
}

#[tauri::command]
pub fn show_panel(handle: AppHandle) {
  let panel = handle.get_webview_panel("main").unwrap();
  panel.show();
}

#[tauri::command]
pub fn hide_panel(handle: AppHandle) {
  let panel = handle.get_webview_panel("main").unwrap();
  panel.order_out(None);
}

#[tauri::command]
pub fn close_panel(handle: AppHandle) {
  let panel = handle.get_webview_panel("main").unwrap();
  panel.close();
}
